package com.ruoyi.common.utils;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class TreeUtils {

    public static String findRootId(List<LinkedHashMap> dataList) {
        dataList.stream().forEach(row -> {
            row.put("id", row.get("id").toString());
            row.put("pid", row.get("pid").toString());
        });
        List<String> ids = dataList.stream().map(r1 -> r1.get("id").toString()).collect(Collectors.toList());
        LinkedHashMap root = dataList.stream().filter(r -> {
            return !ids.contains(r.get("pid").toString());
        }).findAny().orElse(null);
        if (root == null) {
            return "0";
        } else {
            return (String) root.get("pid");
        }
    }
}
